// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is based on code available under the Apache license here:
//   https://github.com/apache/incubator-doris/blob/master/gensrc/proto/types.proto

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

syntax = "proto2";

package starrocks;
option java_package = "com.starrocks.proto";

message PScalarType {
    // TPrimitiveType, use int32 to avoid redefine Enum
    required int32 type = 1;
    // Only set if type == CHAR or type == VARCHAR
    optional int32 len = 2;
    // Only set for DECIMAL
    optional int32 precision = 3;
    optional int32 scale = 4;
};

// Represents a field in a STRUCT type.
// TODO: Model column stats for struct fields.
message PStructField {
    required string name = 1;
    optional string comment = 2;
};

message PTypeNode {
    // TTypeNodeType(SCALAR, ARRAY, MAP, STRUCT)
    required int32 type = 1;
    // only set for scalar types
    optional PScalarType scalar_type = 2;
    // only used for structs; has struct_fields.size() corresponding child types
    repeated PStructField struct_fields = 3;
};

// A flattened representation of a tree of column types obtained by depth-first
// traversal. Complex types such as map, array and struct have child types corresponding
// to the map key/value, array item type, and struct fields, respectively.
// For scalar types the list contains only a single node.
// Note: We cannot rename this to TType because it conflicts with Thrift's internal TType
// and the generated Python thrift files will not work.
// Note: TTypeDesc in impala is TColumnType, but we already use TColumnType, so we name this
// to TTypeDesc. In future, we merge these two to one
message PTypeDesc {
    repeated PTypeNode types = 1;
};

message PUniqueId {
    required int64 hi = 1;
    required int64 lo = 2;
};

enum CompressionTypePB {
    UNKNOWN_COMPRESSION = 0;
    DEFAULT_COMPRESSION = 1;
    NO_COMPRESSION = 2;
    SNAPPY = 3;
    LZ4 = 4;
    LZ4_FRAME = 5;
    ZLIB = 6;
    ZSTD = 7;
    GZIP = 8;
    DEFLATE = 9;
    BZIP2 = 10;
    LZO = 11; // Deprecated
    BROTLI = 12;
    LZ4_HADOOP = 13;
}

enum WriteQuorumTypePB {
    ONE = 0;
    MAJORITY = 1;
    ALL = 2;
}

enum TxnTypePB {
     TXN_NORMAL = 0;
     TXN_REPLICATION = 1;
 }

 enum ReplicationTxnStatePB {
     TXN_PREPARED = 0;
     TXN_SNAPSHOTED = 1;
     TXN_REPLICATED = 2;
     TXN_PUBLISHED = 3;
 }

 message ReplicationTxnMetaPB {
     optional int64 txn_id = 1;
     optional ReplicationTxnStatePB txn_state = 2;
     optional int64 tablet_id = 3;
     optional int64 visible_version = 4;
     optional string src_backend_host = 5;
     optional int32 src_backend_port = 6;
     optional string src_snapshot_path = 7;
     optional int64 snapshot_version = 8;
     optional bool incremental_snapshot = 9;
 } 

// Used to store additional information about a txn when it is finished/visible
// It will be serialized with TransactionState
message TxnFinishStatePB {
    // Currently, only involved replica's versions are stored, to save space, replica are stored in 2 lists
    //
    // a list of int64 to store `normal` replicas, in the following form:
    //   replica1Id, replica2Id, ...
    //
    // a list of int64 to store `abnormal` replica's versions, in the following form:
    //   replica1Id, replica1Version, replica2Id, replica2Version, ...
    //
    // For all the replicas involved in a txn, if the replica's version != partition's txn commit version
    // it is considered abnormal, and put into abnormal list.
    repeated int64 normal_replicas = 1;
    repeated int64 abnormal_replicas_with_version = 2;
}

message DeletePredicatePB {
    required int32 version = 1;
    repeated string sub_predicates = 2;
    repeated InPredicatePB in_predicates = 3;
    // binary_predicates only set and used by a lake table now.
    repeated BinaryPredicatePB binary_predicates = 4;
    // is_null_predicates only set and used by a lake table now.
    repeated IsNullPredicatePB is_null_predicates = 5;
}

message InPredicatePB {
    optional string column_name = 1;
    optional bool is_not_in = 2;
    repeated string values = 3;
}

message BinaryPredicatePB {
    optional string column_name = 1;
    optional string op = 2;
    optional string value = 3;
}

message IsNullPredicatePB {
    optional string column_name = 1;
    optional bool is_not_null = 2;
}
